let imp = require('../../../utils/util')
import Toast from '@vant/weapp/toast/toast';
import {
    request
} from '../../../utils/api';
import {
    relationshipcolumns,
    careercolumns,
    conditioncolumns
} from '../../../utils/columus';
Page({
    data: {
        userId: 0,
        show: [],
        info: [{}],
        relationshipcolumns: relationshipcolumns,
        careercolumns: careercolumns,
        conditioncolumns: conditioncolumns,
        message: [
            []
        ],
        index: 0,
        flag: false
    },
    open(e) {
        let num = e.currentTarget.dataset.num
        let index = Number.parseInt(num.split('-')[0])
        let field = Number.parseInt(num.split('-')[1])
        this.setData({
            [`show[${index}]`]: !this.data.show[index],
            index: field
        });
    },
    change(e) {
        let num = e.currentTarget.dataset.num
        let index = Number.parseInt(num.split('-')[0])
        let field = num.split('-')[1]
        this.setData({
            [`info[${index}].${field}`]: e.detail
        });
    },
    cancel() {
        this.setData({
            show: []
        });
    },
    plus() {
        let arr = this.data.info
        arr.push({})
        this.setData({
            info: arr
        })
    },
    minus() {
        let arr = this.data.info
        arr.shift()
        this.setData({
            info: arr
        })
    },
    confirm(e) {
        let field = e.currentTarget.dataset.num
        let index = this.data.index
        this.setData({
            [`info[${index}].${field}`]: e.detail.value.id,
            [`info[${index}].${field}Name`]: e.detail.value.text
        });
        this.cancel()
    },
    per() {
        wx.navigateBack()
    },
    async next() {
        this.setData({
            message: []
        })

        let info = this.data.info
        console.log(info);
        info.forEach((item, index) => {
            if (!item.name) {
                this.setData({
                    [`message[${index}][0]`]: '请输入姓名'
                })
            }
            if (!item.age && item.age !== 0) {
                this.setData({
                    [`message[${index}][1]`]: '请输入年龄'
                })
            }
            if (!item.relationship && item.relationship !== 0) {
                this.setData({
                    [`message[${index}][2]`]: '请选择关系'
                })
            }
            if (!item.career && item.career !== 0) {
                this.setData({
                    [`message[${index}][3]`]: '请选择职业'
                })
            }
            if (!item.condition && item.condition !== 0) {
                this.setData({
                    [`message[${index}][4]`]: '请选择健康状况'
                })
            }
        })

        if (this.data.message.length == 0) {
            const user = wx.getStorageSync('user')

            let res = await request({
                url: '/user/family',
                method: 'post',
                data: {
                    userId: user.userId,
                    membersList: this.data.info
                }
            })
            if (res.code == 200) {
                if (this.data.userType == 0 && !this.data.flag) {
                    wx.navigateTo({
                        url: '/pages/details/score/score?id=' + this.data.userId,
                    })
                } else {
                    Toast.success('修改成功');
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 600);
                }
            }
        }

    },
    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        let res = await request({
            url: '/user/getFamilyMembers?userId=' + this.data.userId,
            method: 'get',
        })
        if (res.result[0].membersList.length > 0) {
            let {
                relationshipcolumns,
                careercolumns,
                conditioncolumns
            } = this.data
            let membersList = res.result[0].membersList
            membersList.forEach((item) => {
                relationshipcolumns.forEach(relationship => {
                    if (item.relationship == relationship.id) {
                        item.relationshipName = relationship.text
                    }
                })
                careercolumns.forEach(career => {
                    if (item.career == career.id) {
                        item.careerName = career.text
                    }
                })
                conditioncolumns.forEach(condition => {
                    if (item.condition == condition.id) {
                        item.conditionName = condition.text
                    }
                })
            })
            this.setData({
                info: membersList
            })
        } else {
            this.setData({
                info: [{}]
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.flag) {
            this.setData({
                flag: true
            })
        } else {
            this.setData({
                flag: false
            })
        }
        this.setData({
            userId: options.id
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})